package top.huhuiyu.springboot2.study.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author 胡辉煜
 * 对所有的控制器信息进行日志记录
 */
// @Aspect表示是一个切面类
@Aspect
@Component
@Slf4j
@Order(1000)
public class ControllerLogger implements BaseControllerAop {

  @Before("controller()")
  public void before(JoinPoint point) {
    log.debug("前置切面=========================================");
    log.debug("进入：{}", point.getSignature());
//    log.debug("target:{}", point.getTarget().getClass().getSimpleName());
//    log.debug("this:{}", point.getThis().getClass().getSimpleName());

    // 获取方法详细参数
    Object[] args = point.getArgs();
    if (args == null || args.length == 0) {
      log.debug("没有参数");
      return;
    }
    log.debug("方法的参数信息：");
    for (Object arg : args) {
      log.debug("{} {}", arg.getClass().getSimpleName(), arg);
    }
  }

  @After("controller()")
  public void after(JoinPoint point) {
    log.debug("离开：{}", point.getSignature());
    log.debug("=========================================");
  }

  @AfterReturning(value = "controller()", returning = "result")
  public void afterReturning(JoinPoint point, Object result) {
    log.debug("返回：{}", result);
  }

}
